/*
 * 用ViewFlipper中装载一张图片显示淡入淡出效果，
 * TimerThread 用来控制动画显示完(也就是n秒后)给Handler发送消息，
 * 并且进行本地、网络的一些信息验证工作
 * Handler 根据消息在Acvitity中调用不同的方法，对显示进行控制
 */
package com.android.sq.zouqi.login;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.android.sq.zouqi.Constants;
import com.android.sq.zouqi.R;
import com.android.sq.zouqi.main.mainActivity;
import com.android.sq.zouqi.user.pCtrlUserInfo;

public class pScrWelcome extends Activity implements Callback, OnTouchListener{
	
	private ViewFlipper view_flipper;
	private HandlerThread handler_thread;
	private int animation_time = 3;	// 动画时间2s
	private int current_view_id;
	public pCtrlUserInfo ctrl_user_info;
	
	private ProgressDialog pro_dialog;
	
	public Handler my_handler;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_anim_applogo);
		
		my_handler = new Handler(this);
		ctrl_user_info = new pCtrlUserInfo(this, my_handler);
		pShowAppLogo();
	}
	
	// 显示app的logo - pCB01F01
	public void pShowAppLogo() {
		view_flipper = (ViewFlipper) findViewById(R.id.flipper_app);
		view_flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_in));
		view_flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_out));
		view_flipper.startFlipping();
		current_view_id = R.id.flipper_app;
		
		handler_thread = new HandlerThread(my_handler, animation_time);
		handler_thread.start();
	}
	
	// 显示公司的logo - pCB01F02
	public void pShowCompLogo() {
		this.setContentView(R.layout.main_anim_complogo);
		view_flipper = (ViewFlipper) findViewById(R.id.flipper_comp);
		current_view_id = R.id.flipper_comp;
		//view_flipper.setOnTouchListener(this);
		view_flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_in));
		view_flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_out));
		view_flipper.startFlipping();
		
		// 重新设置handler_thread的startTime，让动画再现一次
		handler_thread.setStartTime(System.currentTimeMillis());
		// 开启下一次动画周期
		handler_thread.setIsAnim(true);
		
		// 开一个线程进行pLogin验证，这是一个网络交互
		Thread loginThread = new Thread(new valideHandler());
		//loginThread.start();
				
		pro_dialog = ProgressDialog.show(pScrWelcome.this, "连接中..",
				"登录中..请稍后....", true, true);
	}
	
	@Override
	public boolean handleMessage(Message msg) {
		// TODO Auto-generated method stub
		switch(msg.what) {
		case Constants.ANIMATION_TIME_UP :
			if (current_view_id == R.id.flipper_app) {
				view_flipper.stopFlipping();
				if (view_flipper != null) {
					view_flipper = null;
				}
				//Log.e("BA", "app logo stopping flipping");
				pShowCompLogo();
			} else if (current_view_id == R.id.flipper_comp) {
				view_flipper.stopFlipping();
				// complogo动画停止，关闭handler_thread运行
				handler_thread.setIsRun(false);
			}
			break;
		case Constants.FINISH_LOGIN :
			this.pGotoMain();
			break;
		case Constants.CHECK_NUM_ERROR :
			if (pro_dialog != null) {
				pro_dialog.dismiss();
			}
			Toast.makeText(pScrWelcome.this, "ID验证失败:\n1.请检查您的网络连接.", //\n2.请联系我们.!",
					Toast.LENGTH_SHORT).show();
			break;
		case Constants.AUTO_REGIST_ERROR :
			if (pro_dialog != null) {
				pro_dialog.dismiss();
			}
			Toast.makeText(pScrWelcome.this, "ID自动注册失败:\n1.请检查您的网络连接.", 
					Toast.LENGTH_SHORT).show();
			break;
		}
		return false;
	}
	
	public void pLogin() {
		Log.e("BA", "pScrWelcom -> pLogin");
		if (!ctrl_user_info.pIfLocalLogin()) {
			String phone_number = ctrl_user_info.pGetLocalCellNumber();
			int user_id = ctrl_user_info.pCheckNumOnServer(phone_number);
			if (user_id == 0) {
				user_id = ctrl_user_info.pUserReg(phone_number);
				pro_dialog = null;
				pro_dialog = ProgressDialog.show(pScrWelcome.this,"注册中...", "您尚未注册，为您自动注册中..请稍后....", true, true);
				
			}
			ctrl_user_info.pUserLogin();
			
			my_handler.sendEmptyMessage(Constants.FINISH_LOGIN);
		}
	}

	// 
	public void pGotoMain() {
		Log.e("BA", "pScrWelcome -> pGotoMain");
		if (ctrl_user_info.isFinishLogin) {
			Intent intent = new Intent();
			intent.setClass(pScrWelcome.this, mainActivity.class);
			this.startActivity(intent);
			// 取消进度对话框
			pro_dialog.dismiss();
			// 跳转到主界面，停止线程
			//handler_thread.setIsRun(false);
		}
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent e) {
		// TODO Auto-generated method stub
		//Log.e("BA", "TOUCH");
		if (v.getId() == R.id.flipper_comp) {
			// 跳转到主界面
			switch(e.getAction()) {
			case MotionEvent.ACTION_DOWN:
				Log.e("BA", "跳转到登录界面");
				break;
			}
		}
		return true;
	}
	
	class valideHandler implements Runnable {
		@Override
		public void run() {
			pLogin();
		}
	}
}
